package com.vincce.fitness.api;
import com.vincce.fitness.constant.WechatConstant;
import com.vincce.fitness.core.util.AccountUtil;
import com.vincce.fitness.core.util.WechatUtil;
import com.vincce.fitness.entity.Account;
import com.vincce.fitness.entity.Article;
import com.vincce.fitness.entity.User;
import com.vincce.fitness.entity.WechatMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created by henry on 5/5/14.
 */
@Service
public class WechatService {

	@Resource
	UserService userService;

	public WechatMessage process(String body) throws IOException {
		WechatMessage receiveMessage = WechatUtil.xmlToMessage(body);
		WechatMessage returnMessage = null;
		User user = userService.insertOrUpdate(receiveMessage.getFromUserName(),receiveMessage.getToUserName());
		if (receiveMessage.getMsgType().equalsIgnoreCase(WechatConstant.MSG_TYPE_TEXT)) {
			returnMessage = processMsgTypeText(receiveMessage,user);
		} else if (receiveMessage.getMsgType().equalsIgnoreCase(WechatConstant.MSG_TYPE_IMAGE)) {
			returnMessage = processMsgTypeImg(receiveMessage,user);
		} else if (receiveMessage.getMsgType().equalsIgnoreCase(WechatConstant.MSG_TYPE_VOICE)) {
			returnMessage = processMsgTypeVoice(receiveMessage,user);
		} else if (receiveMessage.getMsgType().equalsIgnoreCase(WechatConstant.MSG_TYPE_EVENT)) {
			if (receiveMessage.getEvent().equalsIgnoreCase(WechatConstant.EVENT_SUBSCRIBE)) {
				Account account = AccountUtil.map.get(receiveMessage.getToUserName());
				userService.updateUserInfo(user,account);
				returnMessage = processEventSubscribe(receiveMessage,user);
			} else if (receiveMessage.getEvent().equalsIgnoreCase(WechatConstant.EVENT_UNSUBSCRIBE)) {
				userService.updateSubscribe(user);
			} else if (receiveMessage.getEvent().equalsIgnoreCase(WechatConstant.EVENT_VIEW)) {
				returnMessage = processEventView(receiveMessage,user);
			} else if (receiveMessage.getEvent().equalsIgnoreCase(WechatConstant.EVENT_CLICK)) {
				returnMessage = processEventClick(receiveMessage,user);
			} else {
				returnMessage = processDefault(receiveMessage,user);
			}
		} else {
			returnMessage = processDefault(receiveMessage,user);
		}
		return returnMessage;
	}

	/**
	 * 菜单view视图
	 * @param receiveMessage
	 * @param user
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	private WechatMessage processEventView(WechatMessage receiveMessage,User user) throws UnsupportedEncodingException {
		WechatMessage returnMessage = createReturnMessage(receiveMessage);
		return returnMessage;
	}

	/**
	 * 菜单点击处理
	 * @param receiveMessage
	 * @param user
	 * @return
	 */
	private WechatMessage processEventClick(WechatMessage receiveMessage,User user) throws UnsupportedEncodingException {
		WechatMessage returnMessage = createReturnMessage(receiveMessage);
        returnMessage.setMsgType("news");
        String key = receiveMessage.getEventKey();
		return returnMessage;
	}



	/**
	 * 订阅处理
	 * @param receiveMessage
	 * @param user
	 * @return
	 */
	private WechatMessage processEventSubscribe(WechatMessage receiveMessage,User user) throws IOException {
		WechatMessage returnMessage = createReturnMessage(receiveMessage);
		return returnMessage;
	}

	/**
	 * 上行文字处理
	 * @param receiveMessage
	 * @param user
	 * @return
	 */
	private WechatMessage processMsgTypeText(WechatMessage receiveMessage,User user) throws UnsupportedEncodingException {
        WechatMessage returnMessage = createReturnMessage(receiveMessage);
        return returnMessage;
	}


	/**
	 * 上行语音处理
	 * @param receiveMessage
	 * @param user
	 * @return
	 */
	private WechatMessage processMsgTypeVoice(WechatMessage receiveMessage,User user) {
		return sendText(receiveMessage, "收到一段语音 : " + receiveMessage.getMediaId() + " " + receiveMessage.getRecognition());
	}

	/**
	 * 上行图片处理
	 * @param receiveMessage
	 * @param user
	 * @return
	 */
	private WechatMessage processMsgTypeImg(WechatMessage receiveMessage,User user) {
		return sendText(receiveMessage, "收到一张图片 : " + receiveMessage.getPicUrl());
	}


	/**
	 * 默认处理
	 * @param receiveMessage
	 * @param user
	 * @return
	 */
	private WechatMessage processDefault(WechatMessage receiveMessage,User user) {
		return sendText(receiveMessage, "默认处理方法");
	}


	/**
	 * 组合下发头消息
	 * @param receiveMessage
	 * @return
	 */
	private WechatMessage createReturnMessage(WechatMessage receiveMessage) {
		WechatMessage returnMessage = new WechatMessage();
		returnMessage.setToUserName(receiveMessage.getFromUserName());
		returnMessage.setFromUserName(receiveMessage.getToUserName());
		returnMessage.setCreateTime((int) (System.currentTimeMillis() / 1000));
		return returnMessage;
	}


	/**
	 * 发送文字消息
	 * @param receiveMessage
	 * @param content
	 * @return
	 */
	private WechatMessage sendText(WechatMessage receiveMessage, String content) {
		WechatMessage returnMessage = createReturnMessage(receiveMessage);
		returnMessage.setMsgType("text");
		returnMessage.setContent(content);
		return returnMessage;
	}

	/**
	 * 发送音频
	 * @param returnMessage
	 * @param id 音频id
	 */
	private void sendAudio(WechatMessage returnMessage, Integer id) {
		WechatMessage.Music music = new WechatMessage.Music();
		returnMessage.setMusic(music);
		returnMessage.setMsgType("music");
	}


	/**
	 * 图文组合第一步
	 * @param id 图文Id
	 * @param userId 用户Id
	 */
	private void sendMultiImg(WechatMessage returnMessage, Integer id,Integer userId) {

	}


	/**
	 * 图文组合第二步
	 * @param returnMessage
	 * @param userId
	 */
	private void addArticle(WechatMessage returnMessage, Article article,Integer userId) {
		returnMessage.addArticle(contentManageToArticle(article,userId));
		if (article.getMultiple().equals(1)) {
			for (Article art : article.getChildList()) {
				returnMessage.addArticle(contentManageToArticle(art,userId));
			}
		}
	}

	/**
	 * 图文组合第三步
	 * @param contentManage
	 * @param userId
	 * @return
	 */
	private WechatMessage.Article contentManageToArticle(Article contentManage,Integer userId) {
		WechatMessage.Article article = new WechatMessage.Article();
		article.setTitle(contentManage.getTitle());
		article.setPicUrl(WechatConstant.BASE_SERVER_URL + "/" + contentManage.getTitleImg());
		article.setDescription(contentManage.getSummary());
		return article;
	}

}
